﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MaximalPath
{
    class Node
    {
        public int value;
        public List<Node> children;
        public Node(int value)
        {
            this.value = value;
            this.children = new List<Node>();
        }
        public void AddChild(Node child)
        {
            this.children.Add(child);
        }
        public int ChildCount
        {
            get
            {
                return this.children.Count;
            }
        }
    }
    class MaximalPath
    {
        static long maxSum = 0;
        static HashSet<Node> usedNodes = new HashSet<Node>();
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            Dictionary<int, Node> Nodes = new Dictionary<int, Node>();
            for (int i = 0; i < n - 1; i++)
            {
                string input = Console.ReadLine();
                int middle = input.IndexOf(" <- ");
                int parentValue = int.Parse(input.Substring(1, middle));
                int childValue = int.Parse(input.Substring(middle + 4, input.Length - 1 - middle - 4));
                Node parent, child;
                if (!Nodes.ContainsKey(parentValue))
                {
                    parent = new Node(parentValue);
                    Nodes.Add(parentValue, parent);
                }
                else
                {
                    parent = Nodes[parentValue];
                }
                if (!Nodes.ContainsKey(childValue))
                {
                    child = new Node(childValue);
                    Nodes.Add(childValue, child);
                }
                else
                {
                    child = Nodes[childValue];
                }
                parent.AddChild(child);
                child.AddChild(parent);
            }


            foreach (var node in Nodes)
            {
                if (node.Value.ChildCount == 1)
                {
                    DFS(node.Value, 0);
                    usedNodes.Clear();
                }
            }
            Console.WriteLine(maxSum);
        }

        private static void DFS(Node node, long sum)
        {
            sum += node.value;
            usedNodes.Add(node);
            if (maxSum< sum)
            {
                maxSum = sum;
            }
            foreach (var child in node.children)
            {
                if (!usedNodes.Contains(child))
                {
                    DFS(child, sum);
                }
            }
        }
    }
}
